﻿
///////////////////////////////////////////////////////////////////////////////////
/// 文 件 名: NeedsOfBorrowerControl.cs
/// 作    者: XXD
/// 创建日期: 2012/6/18 9:48:18
/// 
///---------------------------------修改记录--------------------------------------
/// 修 改 人:          
/// 修改日期:
/// 修改原因: 
//////////////////////////////////////////////////////////////////////////////////
namespace ZX.WebApp.WorkFlow.CustomForms
{
    using ZX.BPM.Library;
    using System.Web;
    using System.Web.UI;

    /// <summary>
    /// (借款，需求，报销)申请单控件
    /// </summary>
    public partial class NeedsOfBorrowerControl : UserControl
    {

        protected void cmbApplyType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cmbApplyType.Text.Equals("物品采购"))
            {
                this.txtLoanAmount.Visible = false;
                this.cmbLoanType.Visible = false;
            }
            else
            {
                this.txtLoanAmount.Visible = true;
                this.cmbLoanType.Visible = true;
            }
        }

        #region 私有方法

        /// <summary>
        /// 设置只读
        /// </summary>
        /// <param name="isRead">是否只读</param>
        void SetReadonly(bool isRead)
        {
            txtSubject.ReadOnly = isRead;
            txtDemandTotalAmount.ReadOnly = isRead;
            txtLoanAmount.ReadOnly = isRead;
            txtRemarks.ReadOnly = isRead;
            cmbApplyType.Enabled = !isRead;
            cmbLoanType.Enabled = !isRead;

            //  FileUploader.ReadOnly = isRead;
        }


        /// <summary>
        /// 获取字符参数
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>返回字符参数</returns>
        string GetStringFromQueryString(string key, string defaultValue)
        {
            string queryStringValue = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(queryStringValue))
            {
                return defaultValue;
            }

            return queryStringValue;
        }

        #endregion

        #region 外部接口

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="data">数据源</param>
        /// <param name="toControl">是否同步到控件</param>
        public void Bind(
            NeedsOfBorrowerEdit data,
            bool toControl)
        {
            if (data == null)
            {
                data = NeedsOfBorrowerEdit.New();
            }

            if (toControl)
            {
                txtDemandTotalAmount.Value = data.DemandTotalAmount;
                txtLoanAmount.Value = data.LoanAmount;
                txtRemarks.Value = data.Remarks;
                txtSubject.Text = data.Subject;
                cmbApplyType.Text = data.ApplyType;
                cmbLoanType.Text = data.LoanType;
                lblCreatedTime.Text = data.CreatedTime;
                lblDeptName.Text = data.DeptName;
                lblFiliale.Text = data.Filiale;
                lblPostName.Text = data.PostName;
                lblProposer.Text = data.Proposer;
                lblNo.Text = data.No;
            }
            else
            {
                decimal demandTotalAmount = 0;
                decimal.TryParse(txtDemandTotalAmount.Value.ToString(), out demandTotalAmount);
                data.DemandTotalAmount = demandTotalAmount;

                decimal loanAmount = 0;
                decimal.TryParse(txtLoanAmount.Value.ToString(), out loanAmount);
                data.LoanAmount = loanAmount;

                data.Remarks = txtRemarks.Text;
                data.Subject = txtSubject.Text;
                data.ApplyType = cmbApplyType.Text;
                data.LoanType = cmbLoanType.Text;
                data.CreatedTime = lblCreatedTime.Text;
                data.DeptName = lblDeptName.Text;
                data.Filiale = lblFiliale.Text;
                data.PostName = lblPostName.Text;
                data.Proposer = lblProposer.Text;
            }

            if (cmbApplyType.Text.Equals("物品采购"))
            {
                this.txtLoanAmount.Visible = false;
                this.cmbLoanType.Visible = false;
            }
            else
            {
                this.txtLoanAmount.Visible = true;
                this.cmbLoanType.Visible = true;
            }
        }

        public void InitFileUploader(NeedsOfBorrowerEdit data)
        {
            FileUploader.WorkflowId = data.WorkFlowId;
            FileUploader.Workflowinsid = data.WorkFlowInsId;
            FileUploader.Worktasid = data.WorkTaskId;
            FileUploader.WorktaskInsId = data.WorkTaskInsId;
            FileUploader.WorkflowSharted = !data.IsNew;
        }

        bool readOnly = false;
        /// <summary>
        /// 是否只读
        /// </summary>
        public bool ReadOnly
        {
            set
            {
                SetReadonly(value);
                readOnly = value;
            }
            get
            {
                return readOnly;
            }
        }
        #endregion

        #region 事件处理

        protected void cmbApplyType_Validation(object sender, DevExpress.Web.ASPxEditors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString().Trim()))
            {
                e.ErrorText = "必须填写";
                e.IsValid = false;
            }
        }

        protected void cmbLoanType_Validation(object sender, DevExpress.Web.ASPxEditors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString().Trim()))
            {
                e.ErrorText = "必须填写";
                e.IsValid = false;
            }
        }

        protected void txtDemandTotalAmount_Validation(object sender, DevExpress.Web.ASPxEditors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))
            {
                e.IsValid = false;
                e.ErrorText = "必须填写";
                return;
            }
          
            if (!Csla.Utilities.IsNumeric(e.Value))
            {
                e.IsValid = false;
                e.ErrorText = "金额格式不正确";
                return;
            }
        }

        protected void txtLoanAmount_Validation(object sender, DevExpress.Web.ASPxEditors.ValidationEventArgs e)
        {
            if (e.Value == null || string.IsNullOrEmpty(e.Value.ToString()))
            {
                e.IsValid = false;
                e.ErrorText = "必须填写";
                return;
            }

            if (!Csla.Utilities.IsNumeric(e.Value))
            {
                e.IsValid = false;
                e.ErrorText = "金额格式不正确";
                return;
            }
        }

        protected void cmbLoanType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string text = cmbLoanType.SelectedItem.Text;
            if (text.Equals("无需借款"))
            {
                txtLoanAmount.Text = string.Empty;
                txtLoanAmount.ReadOnly = true;
            }
            else
            {
                txtLoanAmount.ReadOnly = true;
            }
        }

        #endregion


    }
}